﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AIS.SDK.Api.Response;
using AIS.SDK.Handler;
using AIS.SDK.Model;
using AISservice.Common;
using AISservice.Entity;
using Common;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;

namespace AISservice.Controllers
{
    [Route("api/v1/[controller]/[action]")]
    [ApiController]
    public class P58E4312Controller : ControllerBase
    {
        /// <summary>
        /// HB单收货
        /// </summary>
        /// <param name="objparam"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<MQResponse> ReceiveHB(dynamic objparam)
        {
            Log4Net.Debug(objparam);
            Console.WriteLine(objparam);
            //用了新的方式，哈哈方便多了
            P58E4312param param = Common.JsonHelper.JsonToObject<P58E4312param>(((Newtonsoft.Json.Linq.JObject)objparam).ToString());
            MQResponse execresult = new MQResponse() { ResponseCode = "200" };
            int stackId = 0;
            int stateId = 0;
            string rid = "";
            string token = "";
            bool needclose = false;
            try
            {
                if (string.IsNullOrWhiteSpace(param.username) || string.IsNullOrWhiteSpace(param.password))
                {
                    throw new Exception("请求的用户名或密码为空！");
                }
               
                token = SomeHandler.GetToken(param.username, param.password);

                if (string.IsNullOrWhiteSpace(token))
                {
                    throw new MQException("401", "验证失败，请检查用户名密码是否正确！");
                }

                //Console.WriteLine($"username:{param.username} Token:{param.token}");
                if (string.IsNullOrWhiteSpace(param.Tolocation))
                    throw new MQException("301", "收货货位不能为空！");
             
                Console.WriteLine($"{DateTime.Now.ToString()} 查找HB单据 {param.HBOrderno}");
                #region 处理代码
                FormRequest formRequest = new FormRequest()
                {
                    //maxPageSize = "2",
                    returnControlIDs = "1[10,11,12,43,41]",
                    formName = "P58E4312_W58E4312D",
                    version = "MBR002",
                    formServiceAction = "R",
                    bypassFormServiceEREvent = true
                   ,
                    formActions = new FormActionsCollection
                                 (
                                     new FSREvent[]
                                     {
                                      new FSREvent() { controlID="7", command="SetControlValue" ,value=param.HBOrderno },
                                      new FSREvent() { controlID="37", command="SetControlValue" ,value="*" },
                                      new FSREvent() { controlID="21", command="DoAction"  }
                                     }
                                 )
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    action = "open",
                    outputType = "VERSION2",
                    formName = "P58E4312_W58E4312D",
                    stackId = stackId,
                    stateId = stateId,
                    formRequest = formRequest

                };

                AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                //Console.WriteLine(response.JsonString);
                stackId = response.stackId;
                stateId = response.stateId;
                rid = response.rid;
                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                {
                    JObject obj = response.ToJObject();
                    int rowcount = Convert.ToInt16(obj["fs_P58E4312_W58E4312D"]["data"]["gridData"]["summary"]["records"]);
                    Console.WriteLine($"查询到{rowcount - 1}条记录");


                    if (rowcount >= 1)
                    {
                        Console.WriteLine("选择第一条记录，并打开编辑界面");
                        ActionRequest actionRequest = new ActionRequest()
                        {
                            returnControlIDs = "1[382,103,116,40,36,599,126]",
                            formOID = "W58E4312D",
                            formActions = new FormActionsCollection
                                  (
                                      new FSREvent[]
                                      {
                                      new FSREvent() { controlID="1.0", command="SelectRow"   },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                      }
                                  )
                             ,
                            stopOnWarning = "false"
                        };

                        asrequest = new ApplicationStackRequest()
                        {
                            outputType = "VERSION2",
                            allowCache = true,
                            action = "execute",
                            actionRequest = actionRequest,
                            stackId = stackId,
                            stateId = stateId,
                            rid = response.rid
                        };
                        response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                        stackId = response.stackId;
                        stateId = response.stateId;
                        rid = response.rid;
                        if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                        {
                            ///获取结果的总行数
                            obj = response.ToJObject();
                            rowcount = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["summary"]["records"]);
                            List<gridrowupdateevents> gridrows = new List<gridrowupdateevents>();
                            List<gridcolumnevent> gridcols = new List<gridcolumnevent>();

                            gridcols.Add(new gridcolumnevent()
                            {
                                columnID = "126",
                                command = "SetGridCellValue",
                                value = param.Tolocation
                            }
                            );
                            for (int i = 0; i < rowcount; i++)
                            {
                                Console.WriteLine($"正在处理第{i}行  原货位{obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LOCNE1_126"]["value"].ToString()}");
                                stackId = response.stackId;
                                stateId = response.stateId;
                                int rowindex = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["rowIndex"]);
                                //[JSON].fs_P58E4210_W58E4210A.data.gridData.rowset.[0].z_LOCN_132.value
                                //[JSON].fs_P58E4210_W58E4210A.data.gridData.rowset.[0].z_LOCN_132.value
                                //if (obj["fs_P58E4210_W58E4210A"]["data"]["gridData"]["rowset"][i]["z_LOCN_132"]["value"].ToString().Contains( "PRI"))
                                //{

                                gridrows.Add(new gridrowupdateevents()
                                {
                                    gridColumnEvents = gridcols,
                                    rowNumber = rowindex
                                });
                                //}

                            }


                            actionRequest = new ActionRequest()
                            {
                                //returnControlIDs = "1[348,440,257,45,46,47,48,49,50,51]",
                                formOID = "W58E4312A",
                                formActions = new FormActionsCollection
                                     (
                                         new FSREvent[]
                                         {
                                      new FSREvent() { controlID="1.0", command="SelectAllRows" ,gridAction =new GridAction(){
                                          gridID="1",gridRowUpdateEvents=gridrows
                                      } },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                         }
                                     )
                                 ,
                                stopOnWarning = "false"
                            };
                            asrequest = new ApplicationStackRequest()
                            {
                                outputType = "VERSION2",
                                allowCache = true,
                                action = "execute",
                                actionRequest = actionRequest,
                                stackId = response.stackId,//上一次返回的值
                                stateId = response.stateId,
                                rid = response.rid
                            };

                            response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                            needclose = true;
                            stackId = response.stackId;
                            stateId = response.stateId;
                            rid = response.rid;
                            //Console.WriteLine(response.JsonString);
                            Console.WriteLine($"{DateTime.Now.ToString()} {param.HBOrderno}收货完成:{param.Tolocation}  {response.currentApp}");
                            if (response.currentApp.Contains("P58E4312_W58E4312A"))
                            {// 修改不成功，会停留在当前页面
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                                {
                                    //不用做点什么
                                }
                            }
                            else
                            {  //fs_P58E4312_W58E4312D
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                                {

                                    obj = response.ToJObject();
                                    //应该查询到数据
                                    //string NAno = obj["fs_P58E4210_W58E4210E"]["data"]["gridData"]["rowset"]["0"]["z_DOCO_45"].ToString();
                                    execresult.Result = param.HBOrderno;
                                    execresult.Message = response.JsonString;
                                    Log4Net.Info(response.JsonString);
                                }
                            }



                        }

                    }
                    else
                    {
                        throw new MQException("404", $"找不到单据{ param.HBOrderno},请联系管理员！");
                    }
                }
                #endregion
            }
            catch (MQException x)
            {
                Log4Net.Error($"请求异常:{x.Message}");
                Console.WriteLine($"请求异常:{x.Message}");
                execresult.ResponseCode = CommonHandler.GetException(x.Message, "CODE", x.ExceptionCode).ToString();
                execresult.Message = CommonHandler.GetException(x.Message, "DESC", x.Message).ToString();
            }
            finally
            {
                #region Close

                ActionRequest actionRequest = new ActionRequest()
                {
                    //returnControlIDs = "1[17,18,19,20]",
                    formOID = "W58E4312A"
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    outputType = "VERSION2",
                    allowCache = true,
                    action = "close",
                    actionRequest = actionRequest,
                    stackId = stackId,//上一次返回的值
                    stateId = stateId,
                    rid = rid
                };
                if (!string.IsNullOrWhiteSpace(token) && needclose)
                {
                    AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                    //Console.WriteLine(response.JsonString);
                }
                #endregion
            }
            return execresult;
        }




        /// <summary>
        /// HA单收货
        /// </summary>
        /// <param name="objparam"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<MQResponse> ReceiveHA(dynamic objparam)
        {
            Log4Net.Debug(objparam);
            Console.WriteLine(objparam);
            //用了新的方式，哈哈方便多了
            P58E4312param param = Common.JsonHelper.JsonToObject<P58E4312param>(((Newtonsoft.Json.Linq.JObject)objparam).ToString());
            MQResponse execresult = new MQResponse() { ResponseCode = "200" };
            int stackId = 0;
            int stateId = 0;
            string rid = "";
            string token = "";
            bool needclose = false;
            try
            {
                if (string.IsNullOrWhiteSpace(param.username) || string.IsNullOrWhiteSpace(param.password))
                {
                    throw new Exception("请求的用户名或密码为空！");
                }

                token = SomeHandler.GetToken(param.username, param.password);

                if (string.IsNullOrWhiteSpace(token))
                {
                    throw new MQException("401", "验证失败，请检查用户名密码是否正确！");
                }

                //Console.WriteLine($"username:{param.username} Token:{param.token}");
                if (string.IsNullOrWhiteSpace(param.Tolocation))
                    throw new MQException("301", "收货货位不能为空！");

                Console.WriteLine($"{DateTime.Now.ToString()} 查找HA单据 {param.HAOrderno}");
                #region 处理代码
                FormRequest formRequest = new FormRequest()
                {
                    //maxPageSize = "2",
                    returnControlIDs = "1[10,11,12,43,41]",
                    formName = "P58E4312_W58E4312D",
                    version = "MBR001",
                    formServiceAction = "R",
                    bypassFormServiceEREvent = true
                   ,
                    formActions = new FormActionsCollection
                                 (
                                     new FSREvent[]
                                     {
                                      new FSREvent() { controlID="7", command="SetControlValue" ,value=param.HAOrderno },
                                      new FSREvent() { controlID="37", command="SetControlValue" ,value="*" },
                                      new FSREvent() { controlID="21", command="DoAction"  }
                                     }
                                 )
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    action = "open",
                    outputType = "VERSION2",
                    formName = "P58E4312_W58E4312D",
                    stackId = stackId,
                    stateId = stateId,
                    formRequest = formRequest

                };
                // open Application and Query Order
                AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                //Console.WriteLine(response.JsonString);
                stackId = response.stackId;
                stateId = response.stateId;
                rid = response.rid;
                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                {
                    JObject obj = response.ToJObject();
                    int rowcount = Convert.ToInt16(obj["fs_P58E4312_W58E4312D"]["data"]["gridData"]["summary"]["records"]);
                    Console.WriteLine($"查询到{rowcount - 1}条记录");


                    if (rowcount >= 1)
                    {
                        Console.WriteLine("选择第一条记录，并打开编辑界面");
                        ActionRequest actionRequest = new ActionRequest()
                        {
                            returnControlIDs = "1[382,103,116,40,36,599,126]",
                            formOID = "W58E4312D",
                            formActions = new FormActionsCollection
                                  (
                                      new FSREvent[]
                                      {
                                      new FSREvent() { controlID="1.0", command="SelectRow"   },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                      }
                                  )
                             ,
                            stopOnWarning = "false"
                        };

                        asrequest = new ApplicationStackRequest()
                        {
                            outputType = "VERSION2",
                            allowCache = true,
                            action = "execute",
                            actionRequest = actionRequest,
                            stackId = stackId,
                            stateId = stateId,
                            rid = response.rid
                        };
                        response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                        stackId = response.stackId;
                        stateId = response.stateId;
                        rid = response.rid;
                        if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                        {
                            ///获取结果的总行数
                            obj = response.ToJObject();
                            rowcount = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["summary"]["records"]);
                            List<gridrowupdateevents> gridrows = new List<gridrowupdateevents>();
                            List<gridcolumnevent> gridcols = new List<gridcolumnevent>();

                            gridcols.Add(new gridcolumnevent()
                            {
                                columnID = "126",
                                command = "SetGridCellValue",
                                value = param.Tolocation
                            }
                            );
                            for (int i = 0; i < rowcount; i++)
                            {
                                Console.WriteLine($"正在处理第{i}行  原货位{obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LOCNE1_126"]["value"].ToString()}");
                                stackId = response.stackId;
                                stateId = response.stateId;
                                int rowindex = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["rowIndex"]);

                                gridrows.Add(new gridrowupdateevents()
                                {
                                    gridColumnEvents = gridcols,
                                    rowNumber = rowindex
                                });
                                //}

                            }


                            actionRequest = new ActionRequest()
                            {
                                //returnControlIDs = "1[348,440,257,45,46,47,48,49,50,51]",
                                formOID = "W58E4312A",
                                formActions = new FormActionsCollection
                                     (
                                         new FSREvent[]
                                         {
                                      new FSREvent() { controlID="1.0", command="SelectAllRows" ,gridAction =new GridAction(){
                                          gridID="1",gridRowUpdateEvents=gridrows
                                      } },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                         }
                                     )
                                 ,
                                stopOnWarning = "false"
                            };
                            asrequest = new ApplicationStackRequest()
                            {
                                outputType = "VERSION2",
                                allowCache = true,
                                action = "execute",
                                actionRequest = actionRequest,
                                stackId = response.stackId,//上一次返回的值
                                stateId = response.stateId,
                                rid = response.rid
                            };

                            response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                            needclose = true;
                            stackId = response.stackId;
                            stateId = response.stateId;
                            rid = response.rid;
                            //Console.WriteLine(response.JsonString);
                            Console.WriteLine($"{DateTime.Now.ToString()} {param.HAOrderno}收货完成:{param.Tolocation}  {response.currentApp}");
                            if (response.currentApp.Contains("P58E4312_W58E4312A"))
                            {// 修改不成功，会停留在当前页面
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                                {
                                    //不用做点什么
                                }
                            }
                            else
                            {  //fs_P58E4312_W58E4312D
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                                {

                                    obj = response.ToJObject();
                                    //应该查询到数据
                                    //string NAno = obj["fs_P58E4210_W58E4210E"]["data"]["gridData"]["rowset"]["0"]["z_DOCO_45"].ToString();
                                    execresult.Result = param.HBOrderno;
                                    execresult.Message = response.JsonString;
                                    Log4Net.Info(response.JsonString);
                                }
                            }



                        }

                    }
                    else
                    {
                        throw new MQException("404", $"找不到单据{ param.HAOrderno},请联系管理员！");
                    }
                }
                #endregion
            }
            catch (MQException x)
            {
                Log4Net.Error($"请求异常:{x.Message}");
                Console.WriteLine($"请求异常:{x.Message}");
                execresult.ResponseCode = CommonHandler.GetException(x.Message, "CODE", x.ExceptionCode).ToString();
                execresult.Message = CommonHandler.GetException(x.Message, "DESC", x.Message).ToString();
            }
            finally
            {
                #region Close

                ActionRequest actionRequest = new ActionRequest()
                {
                    //returnControlIDs = "1[17,18,19,20]",
                    formOID = "W58E4312A"
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    outputType = "VERSION2",
                    allowCache = true,
                    action = "close",
                    actionRequest = actionRequest,
                    stackId = stackId,//上一次返回的值
                    stateId = stateId,
                    rid = rid
                };
                if (!string.IsNullOrWhiteSpace(token) && needclose)
                {
                    AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                    //Console.WriteLine(response.JsonString);
                }
                #endregion
            }
            return execresult;
        }



        /// <summary>
        /// HA单收货, 按ME码，批号
        /// </summary>
        /// <param name="objparam"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<MQResponse> ReceiveHABySKULOT(dynamic objparam)
        {
            Log4Net.Debug(objparam);
            Console.WriteLine(objparam);
            //用了新的方式，哈哈方便多了
            P58E4312param param = Common.JsonHelper.JsonToObject<P58E4312param>(((Newtonsoft.Json.Linq.JObject)objparam).ToString());
            MQResponse execresult = new MQResponse() { ResponseCode = "200" };
            int stackId = 0;
            int stateId = 0;
            string rid = "";
            string token = "";
            bool needclose = false;
            try
            {


                if (string.IsNullOrWhiteSpace(param.username) || string.IsNullOrWhiteSpace(param.password))
                {
                    throw new Exception("请求的用户名或密码为空！");
                }

                token = SomeHandler.GetToken(param.username, param.password);

                if (string.IsNullOrWhiteSpace(token))
                {
                    throw new MQException("401", "验证失败，请检查用户名密码是否正确！");
                }

                //Console.WriteLine($"username:{param.username} Token:{param.token}");
                if (string.IsNullOrWhiteSpace(param.Tolocation))
                    throw new MQException("301", "收货货位不能为空！");

                Console.WriteLine($"{DateTime.Now.ToString()} 查找HA单据 {param.HAOrderno} 准备收{param.ItemNumber} {param.Lot} {param.Quantity}");
                #region 处理代码
                FormRequest formRequest = new FormRequest()
                {
                    //maxPageSize = "2",
                    returnControlIDs = "1[10,11,12,43,41]",
                    formName = "P58E4312_W58E4312D",
                    version = "MBR001",
                    formServiceAction = "R",
                    bypassFormServiceEREvent = true
                   ,
                    formActions = new FormActionsCollection
                                 (
                                     new FSREvent[]
                                     {
                                      new FSREvent() { controlID="7", command="SetControlValue" ,value=param.HAOrderno },
                                      new FSREvent() { controlID="37", command="SetControlValue" ,value="*" },
                                      new FSREvent() { controlID="21", command="DoAction"  }
                                     }
                                 )
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    action = "open",
                    outputType = "VERSION2",
                    formName = "P58E4312_W58E4312D",
                    stackId = stackId,
                    stateId = stateId,
                    formRequest = formRequest

                };
                // open Application and Query Order
                AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                //Console.WriteLine(response.JsonString);
                stackId = response.stackId;
                stateId = response.stateId;
                rid = response.rid;
                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                {
                    JObject obj = response.ToJObject();
                    int rowcount = Convert.ToInt16(obj["fs_P58E4312_W58E4312D"]["data"]["gridData"]["summary"]["records"]);
                    Console.WriteLine($"查询到{rowcount - 1}条记录");


                    if (rowcount >= 1)
                    {
                        Console.WriteLine("选择第一条记录，并打开编辑界面");
                        ActionRequest actionRequest = new ActionRequest()
                        {
                            returnControlIDs = "1[382,103,116,40,36,599,126,46,44,42,47]",
                            formOID = "W58E4312D",
                            formActions = new FormActionsCollection
                                  (
                                      new FSREvent[]
                                      {
                                      new FSREvent() { controlID="1.0", command="SelectRow"   },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                      }
                                  )
                             ,
                            stopOnWarning = "false"
                        };

                        asrequest = new ApplicationStackRequest()
                        {
                            outputType = "VERSION2",
                            allowCache = true,
                            action = "execute",
                            actionRequest = actionRequest,
                            stackId = stackId,
                            stateId = stateId,
                            rid = response.rid
                        };
                        response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                        stackId = response.stackId;
                        stateId = response.stateId;
                        rid = response.rid;
                        if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                        {
                            Log4Net.Debug("ReceiveHABySKULOT");
                            Log4Net.Debug(response.JsonString);
                            ///获取结果的总行数
                            obj = response.ToJObject();
                            rowcount = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["summary"]["records"]);
                            List<gridrowupdateevents> gridrows = new List<gridrowupdateevents>();
                            

                            
                            for (int i = 0; i < rowcount; i++)
                            {
                                stackId = response.stackId;
                                stateId = response.stateId;
                                List<gridcolumnevent> gridcols = new List<gridcolumnevent>();
                                int rowindex = Convert.ToInt32(obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["rowIndex"]);
                                //
                                string itemnumber = obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LITM_103"]["value"].ToString();
                                string lot = obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LOTN_46"]["value"].ToString();
                                string lineid=  (Convert.ToDecimal( obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LNID_44"]["value"].ToString()) *1000 ).ToString() ;
                                lineid = obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LNID_44"]["value"].ToString();

                                lineid = (Convert.ToDouble(lineid) * 1000).ToString();
                                //Console.WriteLine($"正在处理第{i}行  原货位{obj["fs_P58E4312_W58E4312A"]["data"]["gridData"]["rowset"][i]["z_LOCNE1_126"]["value"].ToString()}");
                                gridcols.Clear();
                                //这里如果是JDE数量小于收货数量，暂时不知道怎么处理
                                //if (param.ItemNumber == itemnumber && param.Lot == lot)
                                if (param.ItemNumber==itemnumber && param.Lot == lot  && param.lineid==lineid )
                                {
                                    Console.WriteLine($"正在处理第{i}行  {lineid}  {itemnumber} {lot}  得收");
                                    gridcols.Add(new gridcolumnevent()
                                    {
                                        columnID = "382",
                                        command = "SetGridCellValue",
                                        value = "1"
                                    }
                                   );
                                    gridcols.Add(new gridcolumnevent()
                                        {   columnID = "126",
                                            command = "SetGridCellValue",
                                            value = param.Tolocation
                                        }
                                    );

                                }
                                else
                                {
                                    Console.WriteLine($"正在处理第{i}行  {lineid} {itemnumber} {lot} 不收");
                                    //不能收货的品种，收货选项应该为空
                                    gridcols.Add(new gridcolumnevent()
                                    {
                                        columnID = "382",
                                        command = "SetGridCellValue",
                                        value = ""
                                    });
                                    gridcols.Add(new gridcolumnevent()
                                        {
                                            columnID = "126",
                                            command = "SetGridCellValue",
                                            value = param.Tolocation
                                        });
                                }


                                gridrows.Add(new gridrowupdateevents()
                                {
                                    gridColumnEvents = gridcols,
                                    rowNumber = rowindex
                                });
                            }


                            actionRequest = new ActionRequest()
                            {
                                //returnControlIDs = "1[348,440,257,45,46,47,48,49,50,51]",
                                formOID = "W58E4312A",
                                formActions = new FormActionsCollection
                                     (
                                         new FSREvent[]
                                         {
                                      new FSREvent() { controlID="1.0", command="SelectAllRows" ,gridAction =new GridAction(){
                                          gridID="1",gridRowUpdateEvents=gridrows
                                      } },
                                      new FSREvent() { controlID="4", command="DoAction"  }
                                         }
                                     )
                                 ,
                                stopOnWarning = "false"
                            };
                            asrequest = new ApplicationStackRequest()
                            {
                                outputType = "VERSION2",
                                allowCache = true,
                                action = "execute",
                                actionRequest = actionRequest,
                                stackId = response.stackId,//上一次返回的值
                                stateId = response.stateId,
                                rid = response.rid
                            };

                            response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                            needclose = true;
                            stackId = response.stackId;
                            stateId = response.stateId;
                            rid = response.rid;
                            //Console.WriteLine(response.JsonString);
                            Console.WriteLine($"{DateTime.Now.ToString()} {param.HAOrderno}收货完成:{param.Tolocation}  {response.currentApp}");
                            if (response.currentApp.Contains("P58E4312_W58E4312A"))
                            {// 修改不成功，会停留在当前页面
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312A"))
                                {
                                    //不用做点什么
                                }
                            }
                            else
                            {  //fs_P58E4312_W58E4312D
                                if (CommonHandler.ValidateReturnValue(response.JsonString, "fs_P58E4312_W58E4312D"))
                                {

                                    obj = response.ToJObject();
                                    //应该查询到数据
                                    //string NAno = obj["fs_P58E4210_W58E4210E"]["data"]["gridData"]["rowset"]["0"]["z_DOCO_45"].ToString();
                                    execresult.Result = param.HAOrderno;
                                    execresult.Message = response.JsonString;
                                    Log4Net.Info(response.JsonString);
                                }
                            }



                        }

                    }
                    else
                    {
                        throw new MQException("404", $"找不到单据{ param.HAOrderno},请联系管理员！");
                    }
                }
                #endregion
            }
            catch (MQException x)
            {
                Log4Net.Error($"请求异常:{x.Message}");
                Console.WriteLine($"请求异常:{x.Message}");
                execresult.ResponseCode = CommonHandler.GetException(x.Message, "CODE", x.ExceptionCode).ToString();
                execresult.Message = CommonHandler.GetException(x.Message, "DESC", x.Message).ToString();
            }
            finally
            {
                #region Close

                ActionRequest actionRequest = new ActionRequest()
                {
                    //returnControlIDs = "1[17,18,19,20]",
                    formOID = "W58E4312A"
                };
                ApplicationStackRequest asrequest = new ApplicationStackRequest()
                {
                    outputType = "VERSION2",
                    allowCache = true,
                    action = "close",
                    actionRequest = actionRequest,
                    stackId = stackId,//上一次返回的值
                    stateId = stateId,
                    rid = rid
                };
                if (!string.IsNullOrWhiteSpace(token) && needclose)
                {
                    AppstackResponse response = AISClientHandler.Instance.Appstack_execute(token, asrequest);
                    //Console.WriteLine(response.JsonString);
                }
                #endregion
            }
            return execresult;
        }
    }
}